<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
    private $_id,$_role;
    public $userlogin;
    const ERROR_NOT_ACTIVATED = 3;

    /**
     * Перезагружаем конструктор с новым параметро userlogin
     * @param string $userlogin
     * @param string $password
     */
    public function __construct($userlogin,$password)
    {
        $this->userlogin = $userlogin;
        $this->password = $password;
    }
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate()
	{
       $record = User::model()->findByAttributes(array('login'=>$this->userlogin));

       if($record === null)
       {
           $this->errorCode = self::ERROR_USERNAME_INVALID;
       }
       elseif($record->password !== $this->hashPass($this->password))
       {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
       }
       elseif($record->active == 0)
       {
           $this->errorCode = self::ERROR_NOT_ACTIVATED;
       }
        else
        {
            $this->_id = $record->id;
            $this->_role =$record->role;
            $this->setState('email',$record->email);
            $this->errorCode = self::ERROR_NONE;
        }
        return !$this->errorCode;
	}
    public function hashPass($pass)
    {
        return md5($pass);
    }
    public function getId()
    {
        return $this->_id;
    }
    public function getRole()
    {
        return $this->_role;
    }
    public function getUserlogin()
    {
        return $this->userlogin;
    }
}